﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TimKiem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            string key_word = Request.QueryString["key"];
            string loai = Request.QueryString["loai"];
            if (key_word != null && key_word != "")
            {
                // SqlDataSource1.SelectCommand = "SELECT [TenTL], [TacGia], [MoTa], [HinhAnh], [DonGia], FROM [TaiLieu] WHERE IDLoaiTL=" + ma_loai;
                switch (loai)
                {
                    case "0":
                        SqlDataSource1.SelectCommand = "SELECT *, (SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE TenTL LIKE N'%" + key_word + "%' OR TacGia LIKE N'%" + key_word + "%' OR MoTa LIKE N'%" + key_word + "%' ORDER by B1.ThuTuHienThi";
                        SqlDataSource2.SelectCommand = "SELECT Count(*) as SoLuong FROM [TaiLieu] B1 WHERE TenTL LIKE N'%" + key_word + "%' OR TacGia LIKE N'%" + key_word + "%' OR MoTa LIKE N'%" + key_word + "%'";
                        break;
                    case "1":
                        SqlDataSource1.SelectCommand = "SELECT *,(SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE TenTL LIKE N'%" + key_word + "%' ORDER by B1.ThuTuHienThi";
                        SqlDataSource2.SelectCommand = "SELECT Count(*) as SoLuong FROM [TaiLieu] B1 WHERE TenTL LIKE N'%" + key_word + "%'";
                        break;
                    case "2":
                        SqlDataSource1.SelectCommand = "SELECT *,(SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE TacGia LIKE N'%" + key_word + "%' ORDER by B1.ThuTuHienThi";
                        SqlDataSource2.SelectCommand = "SELECT Count(*) as SoLuong FROM [TaiLieu] B1 WHERE TacGia LIKE N'%" + key_word + "%'";
                        break;
                    case "3":
                        SqlDataSource1.SelectCommand = "SELECT *,(SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE MoTa LIKE N'%" + key_word + "%' ORDER by B1.ThuTuHienThi";
                        SqlDataSource2.SelectCommand = "SELECT Count(*) as SoLuong FROM [TaiLieu] B1 WHERE MoTa LIKE N'%" + key_word + "%'";
                        break;
                }
                if (SqlDataSource1.SelectCommand.ToString() != "" && SqlDataSource1.SelectCommand.ToString() != null)
                {
                   
                    ListView2.DataSource = SqlDataSource2;
                    ListView2.DataBind();
                    if (!IsPostBack)
                    {
                        ListView1.DataBind();
                        
                    }

                }
                else
                {
                    Response.Redirect("TrangChu.aspx");
                    Response.Clear();
                }
            }
            else
            {
                Response.Redirect("TrangChu.aspx");
                Response.Clear();
            }
        }

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (String.Equals(e.CommandName, "AddToCard"))
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
            string sl = tb.Text;

            int soluong = System.Int32.Parse(sl);

            string IDTaiLieu =
              ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
            string ten = ListView1.DataKeys[dataItem.DisplayIndex].Values["TenTL"].ToString();
            string gia = ListView1.DataKeys[dataItem.DisplayIndex].Values["DonGia"].ToString();
            int dongia = System.Int32.Parse(gia.Replace(".", ""));
            int ID = System.Int32.Parse(IDTaiLieu);
            long thanh_tien = dongia * soluong;
            //ShoppingCart tl = new ShoppingCart(ID,ten,dongia,soluong,thanh_tien);

            //Session.Add("gio_hang",tl);

            ShoppingCart card = (ShoppingCart)Session["gio_hang"];
            if (card == null)
                card = new ShoppingCart();
            card.Add(ID, ten, dongia, soluong, thanh_tien);
            Session["gio_hang"] = card;

            ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
            master.RefreshShoppingCartInfo();
            /*
            if (SelectedEmployeesListBox.Items.FindByValue(IDTaiLieu) == null)
            {
                ListItem item = new ListItem(e.CommandArgument.ToString(), IDTaiLieu);
                SelectedEmployeesListBox.Items.Add(item);
            }
             */
        }
    }

    public string format_tt(string tt)
    {

        string rs = tt;
        //thay dau ',' thanh '.'
        int l = (rs.Length - 1) / 3;
        for (int i = 0; i < l; i++)
        {
            rs = rs.Insert(rs.Length - (i + 1) * 3 - i, ".");
        }

        return rs;
    }
}